package com.example.eeuse.elasticsearch.controller;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;

import com.example.eeuse.elasticsearch.mapper.UsNewsMediaChinaEconmonMapper;

import com.example.eeuse.elasticsearch.model.UsNewsMediaChinaEconmonmy;
import com.example.eeuse.elasticsearch.service.JsonToEsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.log4j.Logger;
import org.dromara.easyes.core.biz.EsPageInfo;

import org.dromara.easyes.core.conditions.select.LambdaEsQueryWrapper;
import org.dromara.easyes.core.core.EsWrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.ArrayList;



/**
 * 测试使用Easy-ES
 * <p>
 * Copyright © 2021 xpc1024 All Rights Reserved
 **/
@Slf4j
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class UsNewsMediaChinaEconomyController {
    private static final Logger logger = Logger.getLogger(UsNewsMediaChinaEconomyController.class);
    private final UsNewsMediaChinaEconmonMapper usnewsmediachinaeconomyMapper;
    private final JsonToEsService jsonToEsService;



    @GetMapping("/createIndexcc")
    public Boolean createIndexcc() {
        // 1.初始化-> 创建索引(相当于mysql中的表)
        return usnewsmediachinaeconomyMapper.createIndex("us_news_media_china_economy_reception");
    }





    /**pageQuery这个方法在传值的时候第一页是1，第二页从2开始，之后以此类推。这个是easy-es的bug，后面需要改正一下。
     *
     * */


    @GetMapping("/testPaginations")
    public void testPaginationQuerys() {
        try {
            LambdaEsQueryWrapper<UsNewsMediaChinaEconmonmy> queryWrapper = EsWrappers.lambdaQuery(UsNewsMediaChinaEconmonmy.class)
                    .eq(UsNewsMediaChinaEconmonmy::getTitle, "我的声明")
                    .and(wrapper -> wrapper
                            .like(UsNewsMediaChinaEconmonmy::getContent, "河北")
                            .or()
                            .like(UsNewsMediaChinaEconmonmy::getContent, "bin")
                    );



            logger.info(usnewsmediachinaeconomyMapper.pageQuery(queryWrapper, 1, 3));

            EsPageInfo<UsNewsMediaChinaEconmonmy> result = usnewsmediachinaeconomyMapper.pageQuery(queryWrapper, 1, 3);
            System.out.println("result = " + result);






            if (result != null && result.getList() != null && !result.getList().isEmpty()) {
                ExcelWriter writer = ExcelUtil.getWriter("/Users/www.macpe.cn/Desktop/easy-es-springboot-demo/writeTest.xlsx");

                // 设置表头
                ArrayList<String> header = CollUtil.newArrayList("ID", "website_name","title","pubtime","author","content","article_url");
                writer.writeRow(header, true);

                // 遍历Document对象，获取属性值并写入Excel
                for (UsNewsMediaChinaEconmonmy usNewsMediaChinaEconmonmy : result.getList()) {
                    ArrayList<? extends Serializable> rowData = CollUtil.newArrayList(
                            usNewsMediaChinaEconmonmy.getId(),
                            usNewsMediaChinaEconmonmy.getWebsite_name(),
                            usNewsMediaChinaEconmonmy.getTitle(),
                            usNewsMediaChinaEconmonmy.getPubtime(),
                            usNewsMediaChinaEconmonmy.getContent(),
                            usNewsMediaChinaEconmonmy.getArticle_url()
                    );
                    writer.writeRow(rowData, true);
                }

                writer.close();
                System.out.println("Excel file written successfully.");
            } else {
                System.out.println("No results found.");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }




    /**
     * 读取json文件批量写入easy-es
     *
     *
     * */


    @GetMapping("/importJsonToEsds")
    public String importJsonToEsds() {
        String jsonFilePath = "/gpadmin/downloads/data.json";
//        String jsonFilePath = "/Users/www.macpe.cn/Downloads/data.json";
        jsonToEsService.readJSON(jsonFilePath);

        return "JSON data imported to Elasticsearch.";
    }



    /**
     *
     * 读取excel文件然后写入es
     *
     *
     * */


    @GetMapping("/importExcelToEsd")
    public String importExcelToEsd(@RequestParam("excelFilePath") String excelFilePath) {

//        String excelFilePath = "/Users/www.macpe.cn/Desktop/datas2.xlsx";

        jsonToEsService.importDataFromExcel(excelFilePath);
        return "excel data imported to Elasticsearch.";
    }





























}
